# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper


  def thumbsmedias_div(medias)
    ret = '<div id="lastthumbs">'
    medias.each do |un_media|
       ret << '<div style="float:left;">'+link_to(image_tag(un_media.img_src, :height => 160),un_media)+'</div>'
     end
    return ret<<'</div>'
  end

  def lastmedias_div(medias)
    previous_categ = nil
    ret = "<div id=\"lastdls\">\n\n"
    medias.sort!{|x,y| x.category_id <=> y.category_id };
    medias.each do |un_media|
      curr_categ = un_media.category
      if (!previous_categ) || (curr_categ.id != previous_categ.id)
        if previous_categ #on ferme la categ precédente si yen avais une
          ret << "</ul></div>\n"
        end
        ret << "<div class=\"last_dls_in_categ\"><h2>#{link_to(curr_categ.label, category_medias_url(curr_categ))}</h2><ul>"
        previous_categ=curr_categ
      end
      if auteur = un_media.user
        ret << "<li>#{link_to(un_media.nom, un_media)}<span style=\"font-size: xx-small;\">&nbsp;tipiak&nbsp;#{link_to(auteur.nickname,user_medias_url(auteur))}</span></li>\n"
      end
    end
    ret << "</ul></div></div>\n"
  end

  def lastmedias_by_category(categ, cnt=10)
    medias = (Media.find(:all, :conditions => "local_path != \"\" AND category_id = '#{categ.id}'", :order => "updated_at DESC", :limit => cnt))
    if(medias && !medias.empty?)
      ret = ""
      first_img_found = false
      thumb_img = ''
      medias.each do |un_media|
          ret << "<li style=\"font-size: x-small;\">#{link_to(un_media.nom, un_media ,:onmouseover =>(un_media.img_src ? "$('thumb_img_c#{categ.id}').src='#{un_media.img_src}';$('thumb_a_c#{categ.id}').href='#{media_url(un_media)}'":'' ))}</li>\n"
          unless first_img_found
            if un_media.img_src && !un_media.img_src.empty?
              thumb_img = '<div style="height:160px;text-align:center;margin-bottom:5px;">'+link_to(image_tag(un_media.img_src,:height=>160, :id => "thumb_img_c#{categ.id}"),un_media, :id =>"thumb_a_c#{categ.id}" )+'</div>'
              first_img_found=true
            end
          end
      end
      return "<div class=\"last_dls_in_categ\"><h2 style=\"text-align:center;\">#{link_to(categ.label, category_medias_url(categ))}</h2>#{thumb_img}<ul>#{ret}</ul></div>"
    end
  end

  def svnchanges_ul
    ret = '<ul class="links">'
    if feed = SVNCHANGES_FEED
      feed.items.each do |item|
       ret << "<li><a href=\"#{item.link.href}\">#{item.title.content}</a></li>\n"
      end
    end
    return ret << '</ul>'
  end

  def top_zeudou_medias(medias)
    if(medias && !medias.empty?)
      ret = ''
      first_img_found = false
      thumb_img = ''
      medias.each do |un_media|
        ret << "<li style=\"font-size: x-small;\">#{un_media.score} - #{link_to(un_media.nom, un_media ,:onmouseover =>(un_media.img_src ? "$('thumb_img_zeudou').src='#{un_media.img_src}';$('thumb_a_zeudou').href='#{media_url(un_media)}'":'' ))}</li>\n"
        unless first_img_found
          if un_media.img_src && !un_media.img_src.empty?
            thumb_img = '<div style="height:160px;text-align:center;margin-bottom:5px;">'+link_to(image_tag(un_media.img_src,:height=>160, :id => "thumb_img_zeudou"),un_media, :id =>"thumb_a_zeudou" )+'</div>'
            first_img_found=true
          end
        end
      end
      return "<div class=\"last_dls_in_categ\"><h2 style=\"text-align:center;\">top zeudou</h2>#{thumb_img}<ul>#{ret}</ul></div>"
    end
  end

  def top_louze_medias(medias)
    if(medias && !medias.empty?)
      ret = ''
      first_img_found = false
      thumb_img = ''
      medias.each do |un_media|
        ret << "<li style=\"font-size: x-small;\">J-#{un_media.score} - #{link_to(un_media.nom, un_media ,:onmouseover =>(un_media.img_src ? "$('thumb_img_loozeu').src='#{un_media.img_src}';$('thumb_a_loozeu').href='#{media_url(un_media)}'":'' ))}</li>\n"
        unless first_img_found
          if un_media.img_src && !un_media.img_src.empty?
            thumb_img = '<div style="height:160px;text-align:center;margin-bottom:5px;">'+link_to(image_tag(un_media.img_src,:height=>160, :id => "thumb_img_loozeu"),un_media, :id =>"thumb_a_loozeu" )+'</div>'
            first_img_found=true
          end
        end
      end
      return "<div class=\"last_dls_in_categ\"><h2 style=\"text-align:center;\">top loozeu</h2>#{thumb_img}<ul>#{ret}</ul></div>"
    end
  end
end
